<template>
  <view class="fault-list-container">
    <!-- 筛选标签 -->
    <view class="filter-tabs">
      <view 
        class="filter-tab" 
        :class="{active: activeFilter === 'all'}"
        @click="changeFilter('all')"
      >
        所有
      </view>
      <view 
        class="filter-tab" 
        :class="{active: activeFilter === 'executing'}"
        @click="changeFilter('executing')"
      >
        待处理
      </view>
      <view 
        class="filter-tab" 
        :class="{active: activeFilter === 'executed'}"
        @click="changeFilter('executed')"
      >
        处理中
      </view>
      <view 
        class="filter-tab" 
        :class="{active: activeFilter === 'completed'}"
        @click="changeFilter('completed')"
      >
        处理完成
      </view>
    </view>

    <!-- 故障数量 -->
    <view class="fault-count">
      <text class="count-text">共{{filteredFaults.length}}条数据</text>
    </view>

    <!-- 故障列表 -->
    <scroll-view class="fault-list" scroll-y>
      <view 
        class="fault-item" 
        v-for="fault in filteredFaults" 
        :key="fault.id"
        @click="goToFaultDetail(fault)"
      >
        <view class="fault-header">
          <view class="fault-number">
            <text class="number-text">故障单号：{{fault.alarmNo}}</text>
            <view class="checkbox" :class="{checked: selectedFaults.includes(fault.id)}" @click.stop="toggleSelect(fault.id)">
              <text class="check-icon" v-if="selectedFaults.includes(fault.id)">✓</text>
            </view>
          </view>
        </view>

        <view class="fault-content">
          <view class="info-grid">
            <view class="info-item">
              <text class="info-label">故障描述：</text>
              <text class="info-value">{{fault.alarmDesc}}</text>
            </view>
            <view class="info-item">
              <text class="info-label">故障类型：</text>
              <text class="info-value">{{fault.alarmType}}</text>
            </view>
            <view class="info-item">
              <text class="info-label">提交时间：</text>
              <text class="info-value">{{fault.alarmTime}}</text>
            </view>
          </view>

          <view class="fault-status">
            <text class="status-text status-executing">{{fault.statusText}}</text>
            <text class="detail-link" @click.stop="goToFaultDetail(fault)">详情</text>
          </view>
        </view>
      </view>

      <!-- 空状态 -->
      <view class="empty-state" v-if="filteredFaults.length === 0">
        <text class="empty-text">暂无故障报修记录</text>
        <button class="empty-btn" @click="goToReport">去报修</button>
      </view>
    </scroll-view>

    <!-- 批量操作栏 -->
    <view class="batch-operations" v-if="selectedFaults.length > 0">
      <view class="batch-info">
        <text class="batch-text">已选择 {{selectedFaults.length}} 个故障单</text>
      </view>
      <view class="batch-actions">
        <button class="batch-btn" @click="batchAssign">批量删除</button>
        <button class="batch-btn" @click="clearSelection">取消选择</button>
      </view>
    </view>

    <!-- 添加按钮 -->
    <view class="add-fault-btn">
      <button class="add-btn" @click="goToReport">添加故障报修</button>
    </view>
  </view>
</template>

<script>
import api from '@/config/api.js'

export default {
  data() {
    return {
      activeFilter: 'all',
      selectedFaults: [],
      faults: []
    }
  },

  computed: {
    filteredFaults() {
      if (this.activeFilter === 'all') {
        return this.faults
      }
      return this.faults.filter(fault => fault.processStatus === this.activeFilter)
    }
  },

  onLoad() {
    this.loadFaultList()
  },

  onPullDownRefresh() {
    this.loadFaultList().finally(() => {
      uni.stopPullDownRefresh()
    })
  },

  methods: {
    async loadFaultList() {
      try {
        const result = await api.fix.getDevicealarm({
          processStatus: this.activeFilter === 'all' ? '' : this.activeFilter
        })
        
        if (result && result.data) {
          this.faults = result.data
        }
      } catch (error) {
        console.error('获取故障列表失败:', error)
      }
    },

    changeFilter(filter) {
      this.activeFilter = filter
      this.loadFaultList()
    },

    toggleSelect(faultId) {
      const index = this.selectedFaults.indexOf(faultId)
      if (index > -1) {
        this.selectedFaults.splice(index, 1)
      } else {
        this.selectedFaults.push(faultId)
      }
    },

    clearSelection() {
      this.selectedFaults = []
    },

    getStatusClass(status) {
      return {
        'status-executing': status == 0,
        'status-executed': status === 1,
        'status-completed': status == 2
      }
    },

    goToFaultDetail(fault) {
      uni.navigateTo({
        url: `/pages/fix/detail?id=${fault.id}`
      })
    },

    goToReport() {
      uni.navigateTo({
        url: '/pages/fix/add'
      })
    },

    batchAssign() {
      if (this.selectedFaults.length === 0) {
        uni.showToast({
          title: '请先选择故障单',
          icon: 'none'
        })
        return
      }

      uni.showModal({
        title: '批量删除',
        content: `确定要删除选中的 ${this.selectedFaults.length} 个故障单吗？`,
        success: async (res) => {
          if (res.confirm) {
            await api.fix.delDevicealarms(this.selectedFaults.map(item=>item.id))
            this.clearSelection()
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.fault-list-container {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 120rpx;
}

/* 筛选标签 */
.filter-tabs {
  display: flex;
  background-color: #fff;
  border-bottom: 1rpx solid #eee;
}

.filter-tab {
  flex: 1;
  text-align: center;
  padding: 24rpx 0;
  font-size: 28rpx;
  color: #666;
}

.filter-tab.active {
  color: #007AFF;
  font-weight: bold;
  position: relative;
}

.filter-tab.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60rpx;
  height: 4rpx;
  background-color: #007AFF;
  border-radius: 2rpx;
}

/* 故障数量 */
.fault-count {
  padding: 30rpx;
  text-align: center;
  background-color: #fff;
  border-bottom: 1rpx solid #eee;
}

.count-text {
  font-size: 26rpx;
  color: #999;
}

/* 故障列表 */
.fault-list {
  height: calc(100vh - 400rpx);
  padding: 20rpx 30rpx;
  box-sizing: border-box;
}

.fault-item {
  background-color: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
}

.fault-header {
  padding: 30rpx 30rpx 0;
}

.fault-number {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.number-text {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
}

.checkbox {
  width: 36rpx;
  height: 36rpx;
  border: 2rpx solid #ddd;
  border-radius: 6rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.checkbox.checked {
  background-color: #007AFF;
  border-color: #007AFF;
}

.check-icon {
  color: #fff;
  font-size: 24rpx;
  font-weight: bold;
}

/* 故障内容 */
.fault-content {
  padding: 20rpx 30rpx 30rpx;
}

.info-grid {
  margin-bottom: 20rpx;
}

.info-item {
  display: flex;
  margin-bottom: 12rpx;
  font-size: 26rpx;
}

.info-label {
  color: #999;
  width: 160rpx;
}

.info-value {
  color: #333;
  flex: 1;
}

.fault-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 20rpx;
  border-top: 1rpx solid #f0f0f0;
}

.status-text {
  font-size: 24rpx;
  padding: 6rpx 12rpx;
  border-radius: 8rpx;
}

.status-executing {
  background-color: #fff7e6;
  color: #fa8c16;
}

.status-executed {
  background-color: #e6f7ff;
  color: #1890ff;
}

.status-completed {
  background-color: #f6ffed;
  color: #52c41a;
}

.detail-link {
  font-size: 26rpx;
  color: #007AFF;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 100rpx 30rpx;
  background-color: #fff;
  border-radius: 16rpx;
}

.empty-text {
  display: block;
  font-size: 28rpx;
  color: #999;
  margin-bottom: 40rpx;
}

.empty-btn {
  background-color: #1890ff;
  color: #fff;
  border-radius: 40rpx;
  padding: 20rpx 60rpx;
  font-size: 28rpx;
}

/* 批量操作栏 */
.batch-operations {
  position: fixed;
  bottom: 120rpx;
  left: 0;
  right: 0;
  background-color: #e6f7ff;
  padding: 20rpx 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1rpx solid #bae7ff;
}

.batch-text {
  font-size: 28rpx;
  color: #1890ff;
  font-weight: bold;
}

.batch-actions {
  display: flex;
  gap: 20rpx;
}

.batch-btn {
  background-color: #1890ff;
  color: #fff;
  padding: 12rpx 24rpx;
  border-radius: 6rpx;
  font-size: 24rpx;
}

/* 添加按钮 */
.add-fault-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  padding: 20rpx 30rpx;
  border-top: 1rpx solid #eee;
}

.add-btn {
  background-color: #1890ff;
  color: #fff;
  height: 80rpx;
  border-radius: 40rpx;
  font-size: 32rpx;
  line-height: 80rpx;
}
</style>